package com.vecspace.dayu.code.service;

import org.springframework.stereotype.Service;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.vecspace.dayu.code.dao.UserPropertyDao;
import com.vecspace.dayu.code.pojo.UserProperty;

/**功能描述 DAYU-用户配置信息的增、删、改查等业务操作的service类
 * @author wanghua
 */
@Service("dayu_userPropertyService")
public class UserPropertyService {
	/** 根据ID查询 DAYU-用户配置信息 
	 * @param id   主键值
	 * @return UserProperty
	 * @throws Exception
	 * @author wanghua
	 */
	public UserProperty getUserProperty(Long id) throws Exception{
		return userPropertyDao.getUserProperty(id);
	}
	
	/**返回用户的配置属性，可能为null
	 * 
	 * @param loginId
	 * @param propName
	 * @return
	 * @throws Exception 
	 * @author wanghua
	 */
	public UserProperty getUserProperty(String loginId, String propName) throws Exception{
		UserProperty prop = userPropertyDao.getUserProperty(loginId, propName);
		return prop;
	}

	/**获取用户配置属性的整数属性（可能为null)
	 * 
	 * @param loginId
	 * @param propName
	 * @return
	 * @throws Exception 
	 * @author wanghua
	 */
	public Long getUserPropertyNum(String loginId, String propName) throws Exception{
		UserProperty prop = userPropertyDao.getUserProperty(loginId, propName);
		if(prop == null)return null;
		return prop.getValueNum();
	}
	/**保存（插入或者更新）用户属性值
	 * 
	 * @param loginId
	 * @param propName
	 * @param value
	 * @throws Exception 
	 * @author wanghua
	 */
	public void saveOrUpdateUserProperty(String loginId, String propName, Long value) throws Exception {
		if(StringUtils.isEmpty(loginId)) return;
		if(StringUtils.isEmpty(propName)) return;
		loginId = loginId.trim().toLowerCase();
		
		UserProperty prop = userPropertyDao.getUserProperty(loginId, propName);
		if(prop == null) {
			prop = new UserProperty();
			prop.setLoginId(loginId);
			prop.setPropName(propName);
		}
		prop.setValueNum(value);
		userPropertyDao.saveUserProperty(prop);
	}
	
	/** 根据ID删除 DAYU-用户配置信息 
	 * @param id	主键值
	 * @return void
	 * @throws Exception
	 * @author wanghua
	 */
	public void deleteUserProperty(Long id) throws Exception{		
		userPropertyDao.deleteUserProperty(id);
	}
	
	/** property **/
	@Autowired
	private UserPropertyDao userPropertyDao;
}
